<template>
  <div class="login-container">
    <!-- 登录表单 -->
    <el-card class="login-card">
      <div class="text-center relative">
        <h2>{{ title }}</h2>
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginData"
        :rules="loginRules"
        class="login-form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <div class="input-wrapper">
            <!-- <i-ep-user class="mx-2" /> -->
            <el-icon class="mx-2">
              <ElIconUser />
            </el-icon>
            <el-input
              ref="username"
              v-model="loginData.username"
              placeholder="用户名"
              name="username"
              size="large"
              class="h-[48px]"
            />
          </div>
        </el-form-item>

        <!-- 密码 -->
        <el-tooltip
          :visible="isCapslock"
          content="大写锁定已打开"
          placement="right"
        >
          <el-form-item prop="password">
            <div class="input-wrapper">
              <!-- <i-ep-lock class="mx-2" /> -->
              <el-icon class="mx-2">
                <ElIconLock />
              </el-icon>
              <el-input
                v-model="loginData.password"
                placeholder="密码"
                type="password"
                name="password"
                @keyup="checkCapslock"
                @keyup.enter="handleLoginSubmit"
                size="large"
                class="h-[48px] pr-2"
                show-password
              />
            </div>
          </el-form-item>
        </el-tooltip>

        <!-- 验证码 -->
        <!-- <el-form-item prop="captchaCode">
          <div class="input-wrapper">
            <svg-icon icon-class="captcha" class="mx-2" />
            <el-input
              v-model="loginData.captchaCode"
              auto-complete="off"
              size="large"
              class="flex-1"
              placeholder="验证码"
              @keyup.enter="handleLoginSubmit"
            />

            <el-image
              @click="getCaptcha"
              :src="captchaBase64"
              class="captcha-image"
            />
          </div>
        </el-form-item> -->

        <!-- 登录按钮 -->
        <el-button
          :loading="loading"
          type="primary"
          size="large"
          class="w-full"
          @click.prevent="handleLoginSubmit"
        >
          登录
        </el-button>
      </el-form>
    </el-card>

    <!-- ICP备案 -->
    <div class="icp-info" v-show="icpVisible">
      <p>
        Copyright © 2021 - 2024 youlai.tech All Rights Reserved. 有来技术
        版权所有
      </p>
      <p>皖ICP备20006496号-3</p>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from "vue";
// 外部库和依赖
import { useRoute } from "vue-router";

// 内部依赖
import { useUserStore, useManageStore } from "@/stores";
import AuthAPI from "@/api/auth";
import router from "@/router";

// 导入 login.scss 文件
import "@/styles/login.scss";

// 使用导入的依赖和库
const userStore = useUserStore();
const manageStore = useManageStore();
const route = useRoute();

// 按钮 loading 状态
const loading = ref(false);
// 是否大写锁定
const isCapslock = ref(false);
// 验证码图片Base64字符串
const captchaBase64 = ref();
// 登录表单ref
const loginFormRef = ref();

const loginData = ref({
  username: "admin",
  password: "123abc!!",
  captchaKey: "",
  captchaCode: "",
});

const loginRules = computed(() => {
  return {
    username: [
      {
        required: true,
        trigger: "blur",
        message: "请输入用户名",
      },
    ],
    password: [
      {
        required: true,
        trigger: "blur",
        message: "请输入密码",
      },
      {
        min: 6,
        message: "密码不能少于6位",
        trigger: "blur",
      },
    ],
    captchaCode: [
      {
        required: false,
        trigger: "blur",
        message: "请输入验证码",
      },
    ],
  };
});
const title = computed(() => manageStore.settingInfo.sys_name);

/** 获取验证码 */
function getCaptcha() {
  AuthAPI.getCaptcha().then((data) => {
    loginData.value.captchaKey = data.captchaKey;
    captchaBase64.value = data.captchaBase64;
  });
}

/** 登录表单提交 */
function handleLoginSubmit() {
  loginFormRef.value?.validate((valid) => {
    if (valid) {
      loading.value = true;
      userStore
        .login(loginData.value)
        .then(() => {
          const { path, queryParams } = parseRedirect();
          router.push({ path: path, query: queryParams });
        })
        .catch(() => {
        //   getCaptcha();
        })
        .finally(() => {
          loading.value = false;
        });
    }
  });
}

/** 解析 redirect 字符串 为 path 和  queryParams */
function parseRedirect() {
  const query = route.query;
  const redirect = query.redirect ?? "/";

  const url = new URL(redirect, window.location.origin);
  const path = url.pathname;
  const queryParams = {};

  url.searchParams.forEach((value, key) => {
    queryParams[key] = value;
  });

  return { path, queryParams };
}

/** 检查输入大小写 */
function checkCapslock(event) {
  // 防止浏览器密码自动填充时报错
  if (event instanceof KeyboardEvent) {
    isCapslock.value = event.getModifierState("CapsLock");
  }
}

onMounted(() => {
//   getCaptcha();
});
</script>

<style lang="scss" scoped>
.mx-2 {
  margin: 6px;
}
.w-full {
  width: 100%;
}
</style>
